﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace pathcopy {
    public partial class mainWindow : Form {
        public mainWindow() {
            InitializeComponent();
            for (var i = 0; i < checkGroup.Controls.Count; i++) {
                Control item = checkGroup.Controls[i];
                string mainKeyName = @"*\shell\" + (i + 1) + "_pathcopy";
                try {
                    RegistryKey rKey = Registry.ClassesRoot.OpenSubKey(mainKeyName);
                    if (rKey != null) {
                        ((CheckBox)item).Checked = true;
                    } else {
                        ((CheckBox)item).Checked = false;
                    }
                } catch (NullReferenceException) {
                }
            }
        }

        private void settingButton_Click(object sender, EventArgs e) {
            bool createFlag = false;
            bool deleteFlag = false;
            var appPath = "\"" + Application.ExecutablePath + "\"";
            string[] copyType = getCopyType();
            for (var i = 0; i < checkGroup.Controls.Count; i++) {
                Control item = checkGroup.Controls[i];
                string mainKeyName = @"*\shell\" + (i + 1) + "_pathcopy";
                string commandKeyName = mainKeyName + @"\command";
                string commandValue = appPath + " " + (i + 1) + " \"%1\"";
                string folderKeyName = @"Folder\shell\" + (i + 1) + "_pathcopy";
                string folderCommandKeyName = folderKeyName + @"\command";
                if (((CheckBox)item).Checked) {
                    createFlag = true;
                    //メインキーの設定
                    RegistryKey rKey = Registry.ClassesRoot.CreateSubKey(mainKeyName);
                    rKey.SetValue("", copyType[i]);
                    rKey.Close();

                    //commandの設定
                    RegistryKey commandKey = Registry.ClassesRoot.CreateSubKey(commandKeyName);
                    commandKey.SetValue("", commandValue);
                    commandKey.Close();

                    if (i <= 1) {
                        //メインキーの設定
                        RegistryKey folderKey = Registry.ClassesRoot.CreateSubKey(folderKeyName);
                        folderKey.SetValue("", copyType[i]);
                        folderKey.Close();

                        //commandの設定
                        RegistryKey folderCommandKey = Registry.ClassesRoot.CreateSubKey(folderCommandKeyName);
                        folderCommandKey.SetValue("", commandValue);
                        folderCommandKey.Close();
                    }
                } else {
                    deleteFlag = true;
                    //チェックが外されたキーの削除
                    try {
                        RegistryKey rKey = Registry.ClassesRoot.OpenSubKey(mainKeyName);
                        if (rKey != null) {
                            Registry.ClassesRoot.DeleteSubKeyTree(mainKeyName);
                            if (i <= 1) {
                                Registry.ClassesRoot.DeleteSubKeyTree(folderKeyName);
                            }
                        }
                    } catch (NullReferenceException) {
                    }
                }
            }
            string createText = "チェックオンの項目を設定しました。";
            string deleteText = "チェックオフの項目を削除しました。";
            string caption = "設定完了";
            MessageBoxButtons button = MessageBoxButtons.OK;
            MessageBoxIcon icon = MessageBoxIcon.Information;
            if (createFlag && deleteFlag) {
                MessageBox.Show(createText + "\n" + deleteText, caption, button, icon);
            } else if (createFlag && !(deleteFlag)) {
                MessageBox.Show(createText, caption, button, icon);
            } else {
                MessageBox.Show(deleteText, caption, button, icon);
            }
            this.Close();
        }

        private string[] getCopyType() {
            string[] copyTypeStr = {
                                    "フルパスをコピー", "\\を重ねてフルパスをコピー",
                                    "ファイル名をコピー", "フォルダパスをコピー",
                                    "\\を重ねてフォルダパスをコピー"
                                };
            return copyTypeStr;
        }

        private void closeButton_Click(object sender, EventArgs e) {
            this.Close();
        }
    }
}
